/*
 * This file deals with all requests to the
 * /do API, that is any requests other than SignIn
 * and Ping.
 * 
 * Author: John Manero
 * Contributor: Chris Casola
 */
var mRespomnse = require('../model/ResponseEntity');
var uuid = require('node-uuid');

/**
 * Send commands to the DLServer
 * 
 */
var Command = function(app) {
	app.post('/do', function(req, res) {
		var request = req.body;
		var session = req.session;
		
		// Only connect to server after a command/keepalive request
		session.start();
		
		if(!Array.isArray(request)) {
			request = [request];
		}
		
		for(var r = 0; r < request.length; r++) {
			debug.log("Request XML: " + request.toXML());
			session.toServer(request[r]);
		}
		
		if (session.clientBuffer.length > 0) {
			debug.log("	Buffer --> " + JSON.stringify(session.clientBuffer) + " --> Client");
			res.send(session.clientBuffer);
			session.clientBuffer = [];
			session.releaseClient();
		}
	});
};

exports.create = Command;
